package chapter17;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-20 下午5:05
 */
public class SimpleTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTest.class);

    public static void main(String[] args) {
        // 连接redis
//        Jedis jedis = new Jedis("localhost", 6379);
        Jedis jedis = getJedis();
        int i = 0;
        boolean flag = true;
        try {
            // 测试开始时间
            long start = System.currentTimeMillis();
            while (flag) {
                long end = System.currentTimeMillis();
                // 当1s后结束测试
                if (end - start > 1000) {
                    break;
                }
                i++;
                jedis.set("test" + i, String.valueOf(i));
            }
        } finally {
            jedis.close();
        }
        LOGGER.info("redis每秒操作{}次", i);
    }

    private static Jedis getJedis() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        //最大空闲数
        poolConfig.setMaxIdle(50);
        //最大连接数
        poolConfig.setMaxTotal(100);
        //最大等待毫秒数
        poolConfig.setMaxWaitMillis(20000);
        //使用配置创建连接池
        JedisPool pool = new JedisPool(poolConfig, "localhost");
        //从连接池获取单个连接
        return pool.getResource();
    }
}
